package tsli.acm.ctrl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import tsli.acm.dao.GAAgentStructureDAO;
import tsli.acm.dao.GACommissionPositiveDAO;
import tsli.acm.dao.TempAlBonusDetailDAO;
import tsli.acm.dao.TempAlDAO;
import tsli.acm.dao.TempAlFreeLookDAO;
import tsli.acm.dao.TempCountAgentDAO;
import tsli.acm.database.Database;
import tsli.acm.model.TempAl;

public class ALComponentAPIMethod {

//	Double amountPC  = 0.0;
//	int OV = 0;
	

	public boolean calculateAL(String closeYm){
		
		Database db = new Database();
		TempAlDAO tempAlDAO  = new TempAlDAO(db);
		TempAlBonusDetailDAO tempAlBonusDetailDAO = new TempAlBonusDetailDAO(db);
		
		//0.Check that closYm is already calculate or not
		if(tempAlBonusDetailDAO.isAlreadyCalculateAlBonus(closeYm)){
			//true for already calculate AL bonus for this closYm
			System.out.println("CLOS_YM : "+closeYm+" is already calcualte AL bonus");
			return true;
		}
		
		
		//1.Find freelook min yyyymm and find list of year to specific each year 
		GACommissionPositiveDAO gaCommissionPositiveDAO = new GACommissionPositiveDAO(db);
		String minClosYM = gaCommissionPositiveDAO.getFreeLookMinClosYYYYMM(closeYm);
		List<String> intervalClosYM = getYYMMBetween(minClosYM,closeYm);
		
		for(String yyymm:intervalClosYM){//Each year yyyymm
			System.out.println("RECAL YEAR : "+yyymm);
			//1.Insert summary sell of AL, that yyyymm to tempAlDAO
			System.out.println("1.Insert summary sell of AL");
			tempAlDAO.calPcOfAL(yyymm);

			//2.Count the agent of each AL of yyyymm
			System.out.println("2.Count the agent of each AL of yyyymm");
			List<Map<String, Object>> countNewAgents1 = gaCommissionPositiveDAO.countAgent(yyymm);
			
			//3.Calculate old Bonus with old pc
			System.out.println("3.Calculate old Bonus with old pc");
			String peNoSm1 = "";
			int countPeNo1 = 0;
			double ov1 = 0;
			double bonus1 = 0;
			List<Map<String, Object>> oldAmountPcs = tempAlDAO.getPcAll(yyymm);//Old PC list born here
			double oldAmountPc = 0;
			
			for(Map<String,Object> row:countNewAgents1){
				peNoSm1 = row.get("PE_NO_SM").toString();
				countPeNo1 = Integer.parseInt(row.get("COUNT_PE_NO").toString());
				ov1 = percentOV(countPeNo1);
				oldAmountPc = findPcInList(oldAmountPcs, peNoSm1);
				bonus1 = oldAmountPc * ov1;
				row.put("PC_OLD", oldAmountPc);
				row.put("OLD_ACTIVE_AMOUNT", countPeNo1);
				row.put("CLOS_YM", yyymm);
				row.put("BONUS_NET",bonus1);//Old bonus is born here
			}
			
			
			
			//4.Delete freelook in tempAl
			System.out.println("4.Delete freelook in tempAl");
			tempAlDAO.deleteFreeLooks(closeYm, yyymm);
			System.out.println("Delete freelook in tempAl");
			
			//5.Add current of free look to tempAlFreeLook
			System.out.println("5.Add current of free look to tempAlFreeLook");
			TempAlFreeLookDAO tfl = new TempAlFreeLookDAO(db);
			tfl.insertCurrentMonthFreelook(yyymm,closeYm);
			
			
			//6.Count new agent (Include activation) , calculate new OV
			System.out.println("6.Count new agent (Include activation) , calculate new OV");
			List<Map<String, Object>> countNewAgents = gaCommissionPositiveDAO.countAgentForReCal(yyymm, closeYm);
			
			
			//7.Calculate new Bonus with new PC
			System.out.println("7.Calculate new Bonus with new PC");
			String peNoSm = "";
			int countPeNo = 0;
			double ov = 0;
			double bonus = 0;
			List<Map<String, Object>> newAmountPcs = tempAlDAO.getPcAll(yyymm);//New PC list born here
			double newAmountPc = 0;
			
//			System.out.println("PC NEW");
//			for(Map<String,Object> row:newAmountPcs){
//				System.out.println(
//				row.get("PE_NO_SM")+" "
//				+row.get("PO_CLOS_YM")+" "
//				+row.get("PC_AL")+" ");
//			}
			
			
			for(Map<String,Object> row:countNewAgents){
				peNoSm = row.get("PE_NO_SM").toString();
				newAmountPc = findPcInList(newAmountPcs, peNoSm);
				countPeNo = Integer.parseInt(row.get("COUNT_PE_NO").toString());
				ov = percentOV(countPeNo);
				bonus = newAmountPc * ov;
				row.put("NEW_ACTIVE_AMOUNT", countPeNo);
				row.put("PC_NEW", newAmountPc);
				row.put("BONUS_FL",bonus);//New bonus is born here
			}
			
			
			
			
//			System.out.println("NEW");
//			for(Map<String,Object> row1:countNewAgents){
//				System.out.println(
//			row1.get("PE_NO_SM")+" "
//			+row1.get("NEW_ACTIVE_AMOUNT")+" "
//			+row1.get("PC_NEW")+" "
//			+row1.get("BONUS_FL")+" ");
//			}
			
			
			//8.Combine to 2 agent list
			System.out.println("8.Combine to 2 agent list");
			for(Map<String,Object> row1:countNewAgents1){//Old agent list
				for(Map<String,Object> row2:countNewAgents){//New agent list
					if(row1.get("PE_NO_SM").equals(row2.get("PE_NO_SM"))
							//&&row1.get("CLOS_YM").equals(row2.get("CLOS_YM"))
							){
						row1.put("BONUS_FL",row2.get("BONUS_FL").equals(null)||row2.get("BONUS_FL")==null?0:row2.get("BONUS_FL"));
						row1.put("PC_NEW", row2.get("PC_NEW").equals(null)||row2.get("PC_NEW")==null?0:row2.get("PC_NEW"));
						row1.put("NEW_ACTIVE_AMOUNT", row2.get("NEW_ACTIVE_AMOUNT").equals(null)||row2.get("NEW_ACTIVE_AMOUNT")==null?0:row2.get("NEW_ACTIVE_AMOUNT"));
					}
				}
			}
			
			for(Map<String,Object> row1:countNewAgents1){
				if(row1.get("PC_NEW")==null){
					System.out.println("Come null");
					row1.put("BONUS_FL",0);
					row1.put("PC_NEW",0);
					row1.put("NEW_ACTIVE_AMOUNT",0);
				}
			}
			
//			System.out.println("OLD");
//			for(Map<String,Object> row1:countNewAgents1){
//				System.out.println(
//			row1.get("CLOS_YM")+" "
//			+row1.get("PE_NO_SM")+" "
//			+row1.get("BONUS_NET")+" "
//			+row1.get("BONUS_FL")+" "
//			+row1.get("PC_OLD")+" "
//			+row1.get("PC_NEW")+" "
//			+row1.get("OLD_ACTIVE_AMOUNT")+" "
//			+row1.get("NEW_ACTIVE_AMOUNT")+" ");
//			}

			//9.Insert to Table: TSLI_TEMP_AL_BONUS_DETAIL
			System.out.println("9.Insert to Table: TSLI_TEMP_AL_BONUS_DETAIL");
			tempAlBonusDetailDAO.InsertValues(countNewAgents1,closeYm);
			
		}
		
		return true;
		
	}
	
	public double findPcInList(List<Map<String, Object>> pcs,String peNoSm1){
		
		for(Map<String, Object> pc:pcs){
			if(pc.get("PE_NO_SM").equals(peNoSm1)||pc.get("PE_NO_SM")==peNoSm1){
				return Double.parseDouble(pc.get("PC_AL").toString());
			}
		}
		return 0;
	}
	
	public List<String> getYYMMBetween(String beginMonth,String endMonth){
		
		System.out.println("beginMonth : "+beginMonth);
		System.out.println("endMonth : "+endMonth);
		
		String beginM = beginMonth;
		String endM = endMonth;
		int beginMInt = beginM==null||beginM.equals(null)?Integer.parseInt(endM):Integer.parseInt(beginM);
		int endMInt = endM==null?beginMInt:Integer.parseInt(endM);
		int interval = endMInt-beginMInt;
		
		List<String> result = new ArrayList<String>();
		
		for(int i=0;i<interval&&beginMInt<Integer.parseInt(endM);i++){
			
			if(beginMInt%100<=12){
				result.add(Integer.toString(beginMInt));
				beginMInt++;
			}
			else{
				int year = Integer.parseInt(beginM.substring(0,4));
				year++;
				beginM = ""+year+"01";
				beginMInt = Integer.parseInt(beginM);
			}
		}
		result.add(endMonth);
		return result;
	}
	
	/**
	 * this get value amount OV on month
	 * @param vaAgent
	 * @return
	 */
	public double percentOV(int vaAgent){
		double act = 0;
		if(vaAgent >= 1 && vaAgent <= 2){ 
			act = 0.14;
		} else if(vaAgent >= 3 && vaAgent <= 4){ 
			act = 0.15;
		} else if(vaAgent >= 5 && vaAgent <= 6){ 
			act = 0.16;
		} else if(vaAgent >= 7 && vaAgent <= 8){ 
			act = 0.17;
		} else if(vaAgent >= 9 && vaAgent <= 10){ 
			act = 0.18;
		} else if(vaAgent >= 11) {
			act = 0.20;
		}
		
		return act;
	}
	
	
	
//	/**
//	 * this count agent 
//	 * @return
//	 */
//	private List<Map<String ,Object>> activeAgentAGAL(Database db){
//		GACommissionPositiveDAO gaCommissionPositiveDAO  = new GACommissionPositiveDAO(db);
//		return gaCommissionPositiveDAO.countAgent(vaClosYm);
//	}
//	
//	/**
//	 * @param db
//	 * @return 
//	 */
//	private List<Map<String ,Object>> sumPcALAG(Database db){
//		TempAlDAO tempAlDAO  = new TempAlDAO(db);
//		return tempAlDAO.sumPcAlAg(vaClosYm);
//	}
	
}
